/**   
* @Package com.app.open.sys.role.web 
*/
package com.wlyy.bcwlw.sys.role.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.role.dao.RoleDAO;
import com.wlyy.bcwlw.sys.role.entity.RoleDTO;
import com.wlyy.bcwlw.sys.role.service.RolePermissionService;
import com.wlyy.bcwlw.sys.role.service.RoleService;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.entity.UserDTO;

/**
 * @ClassName. SysRoleController
 * @Description. TODO
 * @author zhangyang
 * @date 2015年8月11日 下午3:27:25
 * @version V1.0
 */
@RemoteProxy
@Controller
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController{
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private RolePermissionService rolePermissionService;
	@Autowired
	private UserDAO userDAO;
	
	
	@RequestMapping("/roleTree")
	public String roleTreeView(HttpServletRequest request, HttpServletResponse response){
		return "sys/role/role-tree";
	}
	
	
	
	@RequestMapping("/roleHasUsers")
	public String roleHasUsers(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		String roleId = get(request,"roleId");
		
		List<UserDTO> userList = userDAO.queryUsershasRole(roleId);
		
		processJsonData4Grid(request, response, limit, curPage, userList.size(), userList);
		
		return null;
	}
	
	
	@RequestMapping("/loadUsers")
	public void searchUserByNameOrUsermame(HttpServletResponse response,HttpServletRequest request) throws Exception{
		String term= get(request, "term");
		List<UserDTO> userList = userDAO.searchByUsernameOrName(term);
		List<String> items=new ArrayList<String>();
		for(UserDTO user:userList){
			items.add(user.getUserName()+"("+user.getName()+")");
		}
		sendJsonMessage(response, items);
	}
	
	@RequestMapping("/checkNameISExist")
	public String checkNameISExist(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String name = get(request,"name");
		String appId = get(request,"appId");
		String id = get(request,"id");
		
		RoleDTO roleDTO = new RoleDTO();
		roleDTO.setAppId(appId);
		roleDTO.setName(name);
		int count = roleDAO.count(roleDTO);
		String str ="true";
		if(count >0){
			str="false";
		}
		if(StringUtils.isNotEmpty(id)){
			roleDTO = roleDAO.findById(id);
			if(roleDTO!=null&&roleDTO.getName().equals(name)){
				str="true";
			}
		}
		sendMessage(response, str);
		return null;
	}
	
	public TreeNode getAllRoleTreeById(){
		return roleService.getAllRoleTreeById();
	}
	
	
	public RoleDTO saveRoleBaseInfo(RoleDTO roleDTO){
		return roleService.save(roleDTO);
	}
	
	public RoleDTO getRoleIBaseInfo(String id){
		if(StringUtils.isNotEmpty(id)){
			 return roleDAO.findById(id);
		}
		return null;
	}
	
	public Map<String,Object> getRoleInfoById(String id){
		return roleService.getRoleInfoById(id);
	}
	
	public String  savePermissionRelation(String roleId,String[] addPermissionIds,String[] delPermissionIds){
		if(StringUtils.isEmpty(roleId)){
			return "请先保存基本信息";
		}
		roleService.savePermissionRelation(roleId,addPermissionIds,delPermissionIds);
		return "保存成功";
	}
	
	public boolean removeRoleById(String roleId){
		return roleService.removeRoleById(roleId);
	}
	
	public boolean deleteUserWithRoleRelation(String roleId,String userId){
		return roleService.deleteUserWithRoleRelation(roleId, userId);
	}
	
	public boolean addUserWithRoleRelation(String roleId,String userName){
		return roleService.addUserWithRoleRelation(roleId, userName);
	}
	

	

}
